<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="75%" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
        <el-main>
            <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
                <el-row :gutter="1">
                    <el-col :span="12">
                        <el-form-item label="点检项目编码" prop="code">
                            <el-input v-model="form.code" clearable placeholder="点检项目编码"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="点检项目名称" prop="title">
                            <el-input v-model="form.title" clearable placeholder="点检项目名称"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="1">
                     <el-col :span="12">
                        <el-form-item label="点检类型" prop="check_class_id">
                            <el-select v-model="form.check_class_id" @change="change" placeholder="请选择" style="width: 100%">
                                <el-option v-for="item in itemList" :key="item.id" :label="item.title" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="点检部门" prop="dept_id">
                            <el-select v-model="form.dept_id" @change="change" placeholder="请选择点检部门" style="width: 100%" filterable>
                                <el-option v-for="item in depList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="1">
                    <el-col :span="12">
                        <el-form-item label="设备代码" prop="device_id">
                            <div style="display:flex;width:100%">
                                <el-input v-model="device_code" readonly style="width:100%"></el-input>
                                <el-button @click="checkDev" type="primary">选择设备</el-button>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设备名称">
                            <el-input v-model="device_name" readonly></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-col :span="12">
                    <el-form-item label="设备类别">
                        <el-input v-model="device_type" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" clearable type="textarea"></el-input>
                </el-form-item>
                <el-form-item label="点检内容" prop="item">
                    <sc-form-table ref="table" v-model="form.item" :addTemplate="addTemplate" placeholder="暂无数据" height="350" drag-sort>
                        <el-table-column prop="title" label="点检项" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.title" placeholder="请输入点检项"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="con" label="点检标准" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.con" placeholder="请输入点检标准"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="type" label="选项值" align="center">
                            <template #default="scope">
                                <el-select v-model="scope.row.type" @change="change" placeholder="请选择" style="width: 100%">
                                    <el-option label="✓-✗" :value="2"></el-option>
                                    <el-option label="数值" :value="1"></el-option>
                                </el-select>
                            </template>
                        </el-table-column>
                        <!-- <el-table-column prop="sta" label="结论" align="center">
                            <template #default="{row}">
                                <el-radio-group v-model="row.sta" class="ml-4" v-if="row.type==1">
                                    <el-radio :label="0" size="large">✓</el-radio>
                                    <el-radio :label="1" size="large">✗</el-radio>
                                </el-radio-group>
                                <el-input v-model="row.con" placeholder="请输入数值" v-if="row.type==2"></el-input>
                            </template>
                        </el-table-column> -->
                    </sc-form-table>
                </el-form-item>
            </el-form>
        </el-main>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">创建点检项目</el-button>
		</template>
	</el-dialog>
    <device-list v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false" draggable :query="{is_no:4,dept_id:form.dept_id}"></device-list>
    <!-- <select-check v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></select-check> -->
</template>

<script>
import deviceList from '@/views/device/deviceList.vue'
// import selectCheck from './selectCheck';
export default {
  components: { deviceList },
//   components: { selectCheck },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增点检项目',
				edit: '编辑点检项目',
				show: '查看'
			},
            dialog: {
				save: false
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				code: "",
				title: "",
				remark: "",
                item:[],
                device_id:"",
                check_class_id:"",
                dept_id:"",
			},
            device_code:"",
            device_name:"",
            device_type:"",
            //验证规则
			rules: {
				code: [
					{ required: true, message: '请输入点检编号' }
				],
                title: [
					{ required: true, message: '请输入点检名称' }
				],
                dept_id: [
					{ required: true, message: '请选择部门' }
				],
            },
            itemList:[],//维保内容
            addTemplate: {
                value: '',
                type: '',
                need: '',
                need1: '',
            },
	      depList:[]
		}
	},
	mounted() {
        this.getCheckClass()
        // this.getDept()
        this.getInfo()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
        add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add");
			});
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.device.deviceDianJian.project.edit.post(this.form);
					} else {
						res = await this.$API.device.deviceDianJian.project.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        async setData(data) {
			this.form.id = data.id
            var res = await this.$API.device.deviceDianJian.project.detail.post({id:data.id});
			this.form.code = res.data.code
			this.form.title = res.data.title
			this.form.device_id = res.data.device_id
			this.form.check_class_id = res.data.check_class_id
            this.form.dept_id = res.data.dept_id
			// this.form.item = res.data.item
            let a = []
            res.data.item.map(item=>{
                a.push({
                    title:item.title,
                    con:item.con,
                    type:Number(item.type),
                    ask:item.ask,
                })
            })
            this.form.item = a
			this.form.remark = res.data.remark
            this.device_code = data.device_code
            this.device_name = data.device_title
            this.device_type = data.class_title
		},
        // 点检类型
        async getCheckClass(){
            var res = await this.$API.all.getCheckClass.post({is_page:0,type:0});//点检
            this.itemList = res.data;
        },
        typeChange(e){
            this.deviceList.map(item=>{
                if(e==item.id){
                    this.name = item.title,
                    this.type = item.class_title
                }
            })
        },
        // 选择设备
        checkDev(){
            this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
        },
        handleSuccess(data) {
            this.form.device_id = data[0].id
            this.device_code = data[0].code
            this.device_name = data[0].title
            this.device_type = data[0].class_title
        },
        async getDept(e){
            var res = await this.$API.all.getDep.get({is_page:0,org_id:e});
            this.depList = res.data;
        },
        async getInfo(){
            var res = await this.$API.admin.user.info.get();
            this.getDept(res.data.user.org_id)
            if(this.mode=="add"){
                this.form.dept_id = res.data.user.dept_id
            }
        },
	}
}
</script>

<style></style>
